package io.dshubs.archives.service.impl;


import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.dshubs.archives.dao.ArchiveBaseInfoMapper;
import io.dshubs.archives.dao.ArchiveSpecialFeatureMapper;
import io.dshubs.archives.dto.ArchiveBaseCategoryDto;
import io.dshubs.archives.dto.ArchiveSpecialFeatureDto;
import io.dshubs.archives.entity.ArchiveBaseCategory;
import io.dshubs.archives.entity.ArchiveBaseInfo;
import io.dshubs.archives.entity.ArchiveSpecialFeature;
import io.dshubs.archives.service.ArchiveSpecialFeatureService;
import io.dshubs.commons.mybatis.service.impl.BaseServiceImpl;
import io.dshubs.commons.security.context.TenantContext;
import io.dshubs.commons.security.user.SecurityUser;
import io.dshubs.commons.security.user.UserDetail;
import io.dshubs.commons.tools.enums.SuperTenantEnum;
import io.dshubs.commons.tools.utils.ConvertUtils;
import io.dshubs.commons.tools.utils.TreeUtils;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 档案专题表 服务实现类
 * </p>
 *
 * @author tan
 * @since 2021-09-03
 */
@Service
public class ArchiveSpecialFeatureServiceImpl extends BaseServiceImpl<ArchiveSpecialFeatureMapper, ArchiveSpecialFeature> implements ArchiveSpecialFeatureService {

    @Autowired
    private ArchiveBaseInfoMapper archiveBaseInfoMapper;

    @Override
    public List<ArchiveSpecialFeatureDto> list(Map<String, Object> params) {
        Wrapper<ArchiveSpecialFeature> wrapper = getWrapper(params);
        if(wrapper == null) {
            return null;
        }
        List<ArchiveSpecialFeature> archiveBaseClassList = this.baseDao.selectList(wrapper);
        if(CollectionUtils.isEmpty(archiveBaseClassList)) {
            return new ArrayList<>();
        }
        List<ArchiveSpecialFeatureDto> dtos = ConvertUtils.sourceToTarget(archiveBaseClassList, ArchiveSpecialFeatureDto.class);
        return TreeUtils.build(dtos);
    }

    private Wrapper<ArchiveSpecialFeature> getWrapper(Map<String, Object> queryMap){
        UserDetail user = SecurityUser.getUser();

        String archiveId = (String) queryMap.get("archiveId");
        String specialFeature = (String) queryMap.get("specialFeature");
        String archiveCode = (String) queryMap.get("archiveCode");
        QueryWrapper<ArchiveSpecialFeature> queryWrapper = new QueryWrapper<ArchiveSpecialFeature>();

        if(user.getSuperTenant() != SuperTenantEnum.YES.value()) {
            queryWrapper.eq("user_id",SecurityUser.getUserId());
        } else if (user.getSuperTenant() == SuperTenantEnum.YES.value()) {
            queryWrapper.eq("tenant_code", TenantContext.getTenantCode(user));
        }

        if(StringUtils.isNotEmpty(specialFeature)){
            queryWrapper.eq("special_feature",specialFeature);
        }
        if(StringUtils.isNotEmpty(archiveCode)){
            QueryWrapper<ArchiveBaseInfo> baseInfoQueryWrapper = new QueryWrapper<ArchiveBaseInfo>().eq("code", archiveCode);

            if(user.getSuperTenant() != SuperTenantEnum.YES.value()) {
                baseInfoQueryWrapper.eq("tenant_code",user.getTenantCode());
            }
            List<ArchiveBaseInfo> archiveBaseInfoList = archiveBaseInfoMapper.selectList(
                    baseInfoQueryWrapper.eq("code",archiveCode));

            if(CollectionUtils.isNotEmpty(archiveBaseInfoList)) {
                queryWrapper.eq("archive_id",archiveBaseInfoList.get(0).getId());
            } else {
                return null;
            }
        }
        if(StringUtils.isNotEmpty(archiveId) ) {
            queryWrapper.eq("archive_id",archiveId);
        }
        return queryWrapper;
    }
}
